import {PatchState, Transaction} from "@valuation/rivet";
import {Editor} from "@valuation/core";


type subsctibeCallback = (
  tr: Transaction,
  oldState: PatchState,
  newState: PatchState
) => void;
export default function subscribeOnUpdates(
  editorView: Editor,
  callback: subsctibeCallback
) {
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  const dispatch = editorView.dispatch.bind(
    editorView
  );

  const handler = async function (tr: Transaction) {
    const oldState = editorView.state;
    await dispatch(tr);
    callback(tr, oldState, editorView.state);
  };

  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  editorView.dispatch = handler;
}
